# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>
# Contributor: Mario Finelli <mario at finel dot li>
# Contributor: Aoibhinn di Tori <email@aoibh.in>
# Contributor: Artem Vorotnikov <artem@vorotnikov.me>

_gemname='haml'
pkgname="ruby-${_gemname}"
pkgver=6.0.10
pkgrel=1
pkgdesc='HTML Abstraction Markup Language - A Markup Haiku'
arch=('loong64' 'x86_64')
url='https://haml.info'
license=('MIT')
depends=('ruby' 'ruby-temple' 'ruby-thor' 'ruby-tilt')
options=('!emptydirs')
source=("https://github.com/haml/${_gemname}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('5f8ad99ccb3923d923aabb3de42b74d6d48f5cc7169668635cea058e84c76fb695ed7a338ae638170cf3c735db18dd05da66fc1f1309f9b80a4432e5380c2eda')
b2sums=('7200a134553751d71d1446127117bc4add490f02875d8781e5ecef7140b7adcf1c9cbe2e45e0f859fb80f71ec23d34858f306f683a22f944bc3232b3e744c40a')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  # we don't build from a git checkout
  sed --in-place --regexp-extended 's|git ls-files -z|find -print0|' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install/${_gemdir}/cache/" \
    "tmp_install/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "tmp_install/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "tmp_install/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "tmp_install/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete
}

# needs rails
#check() {
#  cd "${_gemname}-${pkgver}"
#
#  local _gemdir="$(gem env gemdir)"
#
#  GEM_HOME="tmp_install/${_gemdir}" rake test
#}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 MIT-LICENSE --target-directory "${pkgdir}/usr/share/licenses/${pkgname}"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
